package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import jdk.jshell.execution.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */


@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanServiceImpl nursingProjectPlanService;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        // 1.查询护理计划主表数据
        NursingPlan nursingPlan = getById(id);
        if (ObjectUtil.isEmpty(nursingPlan)){
            return null;
        }
        // 将封装在nursingPlan对象的数据拷贝到NursingPlanVo类的对象中，为响应前端数据做第一步准备
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        // 2.查询护理计划关联的护理项目子表数据
        List<NursingProjectPlan> list = nursingProjectPlanService.lambdaQuery()
                .eq(NursingProjectPlan::getPlanId, id)
                .list();
        if (!ObjectUtil.isEmpty(list)){
            // 将封装的数据类型转为NursingProjectPlanVo
            List<NursingProjectPlanVo> projectPlanVoList = list.stream().map(npp -> {
                NursingProjectPlanVo projectPlanVo = BeanUtil.toBean(npp, NursingProjectPlanVo.class);
                projectPlanVo.setProjectId(npp.getProjectId().toString());
                projectPlanVo.setExecuteCycle(npp.getExecuteCycle().toString());
                projectPlanVo.setExecuteFrequency(npp.getExecuteFrequency().longValue());
                return projectPlanVo;
            }).collect(Collectors.toList());
            // 3.继续构建NursingPlanVo并返回
            nursingPlanVo.setProjectPlans(projectPlanVoList);
        }
        return nursingPlanVo;
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 封装了从前端传入的待新增的护理计划数据
     * @return 成功信息
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 1. 保存护理计划主表数据（先将传入的数据中，主表的数据保存）
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        this.save(nursingPlan);  // mybatisPlus 的save 保存完数据后，主键会自动回填
        // 2. 批量保存新增护理计划中与护理项目相关的数据，即子表（护理计划和护理项目关联的中间表）的数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        // 如果projectPlans为空，则直接返回1，表示新增成功并结束方法，因为当projectPlans为空时，表示用户没有填这一项的数据，即不需要进行添加处理
        if (ObjectUtil.isEmpty(projectPlans)){
            return 1;
        }
        // 如果projectPlans不为空，则进行批量保存
        projectPlans.forEach(p -> p.setPlanId(nursingPlan.getId()));  // 先为子表数据批量设置护理计划id，保证其与项目计划表的逻辑关联
        boolean result = nursingProjectPlanService.saveBatch(projectPlans); // 然后将数据批量插入这张关联子表中
        return result ? 1 : 0;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 封装了从前端传入的待新增的护理计划数据
     * @return 成功信息
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        // 1.修改护理计划关联的护理项目子表
        // 1.1 先删除子表数据(无论是否有值，都执行一次删除操作，即重置数据)
//        LambdaQueryWrapper<NursingProjectPlan> eq1 = new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId());
        LambdaQueryWrapper<NursingProjectPlan> eq = Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId());
        nursingProjectPlanService.remove(eq);
        // 1.2 判断前端是否有提交子表数据，如果有，则批量保存
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (ObjectUtil.isNotEmpty(projectPlans)){
            projectPlans.forEach(p -> p.setPlanId(nursingPlanDto.getId()));
            this.nursingProjectPlanService.saveBatch(projectPlans);
        }

        // 3.修改护理计划主表数据
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        return this.updateById(nursingPlan) ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanByIds(Long[] ids)
    {
        // 1.批量删除护理计划主表数据
        boolean a = this.removeByIds(List.of(ids));

        // 2.批量删除护理计划关联的护理项目子表数据
        boolean b = nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().in(NursingProjectPlan::getPlanId, List.of(ids)));

        return a && b ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
//    @Override
//    public int deleteNursingPlanById(Long id)
//    {
//        return removeById(id) ? 1 : 0;
//    }
}
